﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib;
using WFNetLib.NMEA;
namespace RobotControl
{
    public partial class Form_Main : Form
    {
        GPGLL gpgll = new GPGLL();        
        private void GPSCom_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            char rx;
            if (ComClosing[5])
                return;
            ComListening[5] = true;
            while (GPSCom.BytesToRead != 0)
            {
                int irx = GPSCom.ReadByte();
                if (irx == -1)
                    break;
                rx = (char)irx;
                if (gpgll.RxProc(rx))
                {
                    ExternAddText("收到GPS数据:\r\n" + gpgll.AllRx);
                    string str;
                    if (gpgll.State == "A")
                        str = "有效定位";
                    else
                    {
                        str = "无效定位";
                        continue;
                    }
                    ExternChangeParamList(ParamsListAddr.定位状态,str);
                    double d;
                    try
                    {
                        d = Convert.ToDouble(gpgll.Latitude.Substring(0, 2));
                        d = d + (Convert.ToDouble(gpgll.Latitude.Substring(2)) / 60);
                        ExternChangeParamList(ParamsListAddr.纬度, d.ToString("F6"));
                    }
                    catch { continue; }
                    if (gpgll.NorS == "N")
                        str = "北半球";
                    else
                        str = "南半球";
                    ExternChangeParamList(ParamsListAddr.纬度半球, str);
                    try
                    {
                        d = Convert.ToDouble(gpgll.Longitude.Substring(0, 3));
                        d = d + (Convert.ToDouble(gpgll.Longitude.Substring(3)) / 60);
                        ExternChangeParamList(ParamsListAddr.经度, d.ToString("F6"));
                    }
                    catch { continue; }
                    if (gpgll.EorW == "E")
                        str = "东经";
                    else
                        str = "西经";
                    ExternChangeParamList(ParamsListAddr.经度半球, str);
                }
            }
            ComListening[5] = false;
        }
    }
}
